import wx from './globalfun'
import { requestPayment, login } from './wechat'
import fly from '../net/flyio'
import url from '../net/serverurl'

async function wxPay(orderNo, code, payBack) {
    const data = await fly.post(url.URL_MINIPAY, {
        code: code,
        order_no: orderNo
    })
    if (data.successCode === 1) {
        var rtpacakge = data.data.package;
        rtpacakge = JSON.parse(rtpacakge);
        requestPayment({
            'timeStamp': rtpacakge.timeStamp,
            'nonceStr': rtpacakge.nonceStr,
            'package': rtpacakge.package,
            'signType': rtpacakge.signType,
            'paySign': rtpacakge.paySign
        }).then(function (res) {
            console.log(res,111)
            if (payBack) {
                payBack(res)
            }
        }, function (reason) {
            payBack(false)
        })
    } else {
        wx.showShortToast(resData.message)
    }
}
function pay(orderNo, payBack) {
    console.log(orderNo)
    return new Promise((revole, rejects) => {
        login().then(function (res) {
            wxPay(orderNo, res.code, payBack)
        })
    })
}

export default {
    pay
}